<?php
declare (strict_types=1);

namespace app\exam\service\admin;

use app\common\service\BaseService;
use app\common\logic\category\admin\Category;
use app\exam\logic\admin\Module as ModuleLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;
use think\Response;

/**
 * Class Module
 *
 * @package app\exam\service\admin
 */
class Module
{
    /**
     * @Inject()
     *
     * @var ModuleLogic
     */
    protected $moduleLogic;

    /**
     * @Inject()
     * @var Category
     */
    protected $category;

    /**
     * 列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/25
     */
    public function index(): Response
    {
        $map = [];
        // 版块名称
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        // 版块类型
        if (Request::has('module_type', 'get', true) && Request::get('module_type/d', -1) !== -1) {
            $map[] = ['module_type', '=', Request::get('module_type/d')];
        }
        //状态
        if (Request::has('status', 'get', true) && Request::get('status/d', -1) !== -1) {
            $map[] = ['status', '=', Request::get('status/d')];
        }

        $data = $this->moduleLogic->getList($map);
        // 定义字段
        $keyList    = [
            'title'            => ListItem::make()->title('版块名称'),
            'module_type_text' => ListItem::make()->title('版块类型'),
            'status'           => ListItem::make('custom')->title('启用状态'),
            'update_time'      => ListItem::make()->title('更新时间')
        ];
        $moduleType = $this->moduleLogic->getModuleTypeOptions();
        $status     = $this->moduleLogic->getStatusOptions();
        // 搜索字段
        $searchKey = [
            'title'       => FormItem::make()->title(false)->placeholder('版块名称')->maxlength(30),
            'module_type' => FormItem::make('select')->title(false)->placeholder('版块类型')->options($moduleType),
            'status'      => FormItem::make('select')->title(false)->placeholder('启用状态')->options($status),

        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey   = $keyList;
        $list->datas     = $data;
        $list->searchKey = $searchKey;

        return $list->send();
    }

    /**
     * 新建版块
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/26
     */
    public function create()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $this->moduleLogic->createModule($data);

            return BaseService::parseToData([], 1, '新建成功');
        }
        /* 创建课程界面配置 */
        $keyList = $this->keyListInit();
        // 设置表单
        $form = new PageForm();
        // 表单字段
        $form->pageKey     = $keyList;
        $form->saveMethod  = 'POST';
        $form->savePostUrl = '/exam/admin/module/create';

        return $form->send();
    }

    /**
     * 编辑版块
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/26
     *
     * @param int $id
     * @return Response
     */
    public function update(int $id): Response
    {
        if (Request::isPut()) {
            $data = Request::put();
            $this->moduleLogic->editModule($id, $data);

            return BaseService::parseToData([], 1, '编辑成功');
        }

        $data = $this->moduleLogic->getModuleById($id);
        // 创建课程界面配置
        $keyList = $this->keyListInit(false);
        // 设置表单
        $form = new PageForm();
        // 表单字段
        $form->datas       = $data;
        $form->pageKey     = $keyList;
        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/exam/admin/module/' . $id . '/edit';

        return $form->send();
    }

    /**
     * 表单字段信息
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/26
     *
     * @param bool $isCreate
     * @return array
     */
    public function keyListInit(bool $isCreate = true): array
    {
        // 版块类型
        $moduleType = $this->moduleLogic->getModuleTypeOptions(false);
        // 版块类型
        // $category = $this->category->topList(3);
        // 定义字段
        return [
            'title'       => FormItem::make()->title('版块名称')->required(),
            'cover'       => FormItem::make('custom')->title('封面')->limit(1)->required()->help('建议宽高尺寸为345px*90px'),
            'module_type' => FormItem::make('select')->title('版块类型')->placeholder('请选择版块类型')->options($moduleType)->disabled(!$isCreate)->required(),
            // 'category'    => FormItem::make('select')->title('版块分类')->placeholder('请选择版块分类')->options($category)->disabled(!$isCreate)->required(),
        ];
    }

    /**
     * 编辑单个字段信息
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/26
     *
     * @param int    $id
     * @param string $field
     * @return Response
     */
    public function editField(int $id, string $field): Response
    {
        $value  = Request::put($field);
        $module = $this->moduleLogic->editField($id, $field, $value);

        return BaseService::parseToData($module, 1, '更新成功');
    }

    /**
     * 删除
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/26
     *
     * @param int $id
     * @return Response
     */
    public function delete(int $id): Response
    {

        $this->moduleLogic->deleteModule($id);

        return BaseService::parseToData([], 1, '删除成功');
    }

}